package com.spdd.mt.app.aop.sign;

// import com.yongping.admin.common.annotation.FireOperationLog;
// import com.yongping.admin.common.util.PreSecurityUser;
// import com.yongping.admin.common.util.SecurityUtil;
// import com.yongping.admin.modules.system.service.LogInfoService;
// import com.yongping.dto.LogInfo;
// import io.swagger.annotations.ApiOperation;

import com.spdd.mt.biz.manager.model.WebRequest;
import com.spdd.mt.biz.manager.service.SignService;
import com.spdd.mt.common.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 操作日志记录切面
 */
@Slf4j
@Aspect
@Component
public class ValidateSignAop {

    @Autowired
    private SignService signService;

    @Pointcut("@annotation(com.spdd.mt.app.aop.sign.ValidateSign)")
    public void validateSign() {
    }

    @Around("validateSign()")
    public Object getReturning(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取swagger接口说明注解
        MethodSignature methodSign = (MethodSignature) joinPoint.getSignature();
        Method method = methodSign.getMethod();
        // StringBuilder desc = new StringBuilder();

        // ValidateSign validateSign = method.getAnnotation(ValidateSign.class);

        if (!method.isAnnotationPresent(ValidateSign.class)) {
            return joinPoint.proceed();
        }

        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof WebRequest) {
                Map<String, Object> mapRequest = JacksonUtils.toObj(JacksonUtils.toJson(arg), Map.class);
                signService.validateSign(mapRequest);
            }
        }

        return joinPoint.proceed();
    }
}
